package com.example.demo.mapper;

import com.example.demo.model.TorrentUsage;
import org.apache.ibatis.annotations.*;

@Mapper
public interface TorrentUsageMapper {
    @Insert("INSERT INTO torrent_usage_stats (torrent_id, upload_count, download_count, last_updated) " +
            "VALUES (#{torrentId}, 0, 0, NOW()) " +
            "ON DUPLICATE KEY UPDATE torrent_id = torrent_id") // 无实际更新
    void initStats(Long torrentId);

    @Update("UPDATE torrent_usage_stats SET " +
            "upload_count = upload_count + #{increment}, " +
            "last_updated = NOW() " +
            "WHERE torrent_id = #{torrentId}")
    void incrementUploadCount(@Param("torrentId") Long torrentId,
                              @Param("increment") long increment);

    @Update("UPDATE torrent_usage_stats SET " +
            "download_count = download_count + #{increment}, " +
            "last_updated = NOW() " +
            "WHERE torrent_id = #{torrentId}")
    void incrementDownloadCount(@Param("torrentId") Long torrentId,
                                @Param("increment") long increment);

    @Select("SELECT * FROM torrent_usage_stats WHERE torrent_id = #{torrentId}")
    TorrentUsage findByTorrentId(Long torrentId);
}